﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_DLL.Product.database;
using ERP_RORZE_DLL.Product.DAO;
using ERP_RORZE_DLL.Product.Base;
using ERP_RORZE_DLL.Product;

namespace ERP_RORZE.Product.PartData
{
    public partial class Frm_OptionDataEdit : DevExpress.XtraEditors.XtraForm
    {
        ClsOptionDataTable obj = new ClsOptionDataTable();
        ClsPartDataDAO clspartDAO = new ClsPartDataDAO();
        DataTable dt;
        public EventHandler Re_Load;

        public string ID;
        public string InputDate;
        public string PartID;
        public string MachineID;
        public string OptionID;
        public string LastOption;
        public string JigID;
        public string ToolQty;
        public string ProTime;
        public string ClampTime;
        public string TechDate;
        public string UpdateDay;
        public string StaffID;
        public string Note;
        public string AondNote;
        public string Memo;
        public string CLUpdateday;

        public Frm_OptionDataEdit()
        {
            InitializeComponent();
        }

        private void Frm_OptionDataEdit_Load(object sender, EventArgs e)
        {
            //lấy PartID
            dt = clspartDAO.GetAllPartID();
            cbPartID.DisplayMember = "PartNo";
            cbPartID.ValueMember = "PartNo";

            // lấy StaffID
            dt = clspartDAO.GetAllStaffID();
            cbStaffID.DisplayMember = "StaffID";
            cbStaffID.ValueMember = "StaffID";

            // lấy OptionID
            dt = clspartDAO.GetAllOptionID();
            cbOptionID.DisplayMember = "OptionID";
            cbOptionID.ValueMember = "OptionID";

            //
            dt = clspartDAO.GetAllMachineID();
            cbMachineID.DisplayMember = "MachineID";
            cbMachineID.ValueMember = "MachineID";

            //Lấy jigID
            dt = clspartDAO.GetAllJigID();
            cbJigID.DisplayMember = "JigID";
            cbJigID.ValueMember = "jigID";
            
            //điền giá trị lên các trường
            cbJigID.Text = JigID;
            cbMachineID.Text = MachineID;
            cbOptionID.Text = OptionID;
            cbPartID.Text = PartID;
            cbStaffID.Text = StaffID;
            txtAondNote.Text = AondNote;
            txtClampTime.Text = ClampTime;
            txtMemo.Text = Memo;
            txtNote.Text = Note;
            txtProTime.Text = ProTime;
            txtToolQty.Text = ToolQty;
            dateCLUpdateday.Value = DateTime.Parse(UpdateDay);
            dateInputDate.Value = DateTime.Parse(InputDate);
            dateUpdateDay.Value = DateTime.Parse(UpdateDay);
            checkLastOption.Checked = bool.Parse(LastOption);
            checkTechDate.Checked = bool.Parse(TechDate);
        }

        void ThongBao(string name)
        {
            MessageBox.Show("Chưa nhập " + name + ". Vui lòng nhập lại");
        }

        private void btnExit_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnEdit_Click_1(object sender, EventArgs e)
        {
            if (txtAondNote.Text == "")
            {
                ThongBao(clsConstantsdb.Entities.ClsDBOptionData.FieldName.AondNote);
            }
            else if (txtClampTime.Text == "")
            {
                ThongBao(clsConstantsdb.Entities.ClsDBOptionData.FieldName.ClampTime);
            }
            else if (txtMemo.Text == "")
            {
                ThongBao(clsConstantsdb.Entities.ClsDBOptionData.FieldName.Memo);
            }
            else if (txtNote.Text == "")
            {
                ThongBao(clsConstantsdb.Entities.ClsDBOptionData.FieldName.Note);
            }
            else if (txtProTime.Text == "")
            {
                ThongBao(clsConstantsdb.Entities.ClsDBOptionData.FieldName.ProTime);
            }
            else if (txtToolQty.Text == "")
            {
                ThongBao(clsConstantsdb.Entities.ClsDBOptionData.FieldName.ToolQty);
            }
            else
            {
                obj.InputDate = dateInputDate.Value;
                obj.PartID = cbPartID.Text;
                obj.MachineID = cbMachineID.Text;
                obj.OptionID = cbOptionID.Text;
                obj.JigID = cbJigID.Text;
                obj.StaffID = cbStaffID.Text;
                obj.ToolQty = int.Parse(txtToolQty.Text);
                obj.ProTime = float.Parse(txtProTime.Text);
                obj.ClampTime = float.Parse(txtClampTime.Text);
                obj.TechDate = checkTechDate.Checked;
                obj.UpdateDay = dateUpdateDay.Value;
                obj.Note = txtNote.Text;
                obj.AondNote = float.Parse(txtAondNote.Text);
                obj.Memo = txtMemo.Text;
                obj.CLUpdateday = dateCLUpdateday.Value;
                obj.LastOption = checkLastOption.Checked;
                obj.ID = int.Parse(ID);
                clspartDAO.UpdateOptionData(obj);
                this.Re_Load(sender, e);
            }
        }
    }
}